﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.View
{
    public partial class ContainerTreeView : BaseView
    {
        public ContainerTreeView()
        {
            InitializeComponent();
        }
        public override void Init(BaseViewModel viewModel)
        {
            m_viewModel = viewModel;

            this.Dock = DockStyle.Fill;
            this.treeView.ContextMenuStrip = this.contextMenuStrip;

            m_viewModel.PropertyChanged += new PropertyChangedEventHandler(ContainerTreeViewModel_PropertyChanged);
            m_viewModel.OnObjectChanged += new EventHandler<Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs>(ContainerTreeViewModel_OnObjectChanged);

            AddCommand2Context(m_viewModel, this.contextMenuStrip);

            treeView.Nodes.Clear();
            FillContainersTree(null, treeView.Nodes);

            treeView.Nodes[0].Expand();
        }

        private void SetImageIndex(TreeNode treeNode, Mimoza.ObjectLayer.Container cont)
        {
            switch (cont.Type)
            {
                case ContainerType.LocalHost:
                    treeNode.ImageIndex = 16;
                    treeNode.SelectedImageIndex = 16;
                    break;
                case ContainerType.Managed:
                    treeNode.ImageIndex = 13;
                    treeNode.SelectedImageIndex = 13;
                    break;
                case ContainerType.Unmanaged:
                    treeNode.ImageIndex = 10;
                    treeNode.SelectedImageIndex = 10;
                    break;
                case ContainerType.Search_Folders:
                case ContainerType.QueryByConnectionTime:
                case ContainerType.QueryByPlatform:
                    treeNode.ImageIndex = 20;
                    treeNode.SelectedImageIndex = 20;
                    break;
                case ContainerType.Updates:
                    treeNode.ImageIndex = 17;
                    treeNode.SelectedImageIndex = 17;
                    break;
                default:
                    treeNode.ImageIndex = 13;
                    treeNode.SelectedImageIndex = 13;
                    break;
            }
        }

        private void FillContainersTree(object tag, TreeNodeCollection parentNodes)
        {
            foreach (Mimoza.ObjectLayer.Container cont in (m_viewModel as ContainerTreeViewModel).GetChildsContainers(tag as Mimoza.ObjectLayer.Container))
            {
                TreeNode treeNode = parentNodes.Add(GetContainerName(cont));
                treeNode.ContextMenuStrip = this.contextMenuStrip;
                treeNode.Tag = cont;
                SetImageIndex(treeNode, cont);               

                FillContainersTree(cont, treeNode.Nodes);
            }
        }
        private TreeNodeCollection FindTreeNodeCollectionByContainer(TreeNodeCollection nodes, Mimoza.ObjectLayer.Container cont)
        {
            foreach (TreeNode node in nodes)
            {
                if (cont.ID == (node.Tag as Mimoza.ObjectLayer.Container).ID)
                {
                    return node.Nodes;
                }
                TreeNodeCollection nextNodes = FindTreeNodeCollectionByContainer(node.Nodes, cont);
                if (nextNodes != null)
                {
                    return nextNodes;
                }
            }

            return null;
        }
        private TreeNode FindTreeNodeByContainer(TreeNodeCollection nodes, Mimoza.ObjectLayer.Container cont)
        {
            foreach (TreeNode node in nodes)
            {
                if (cont.ID == (node.Tag as Mimoza.ObjectLayer.Container).ID)
                {
                    return node;
                }
                TreeNode nextNode = FindTreeNodeByContainer(node.Nodes, cont);
                if (nextNode != null)
                {
                    return nextNode;
                }
            }

            return null;
        }

        private void InsertContaner(Mimoza.ObjectLayer.Container cont)
        {
            TreeNodeCollection parentNodes = FindTreeNodeCollectionByContainer(treeView.Nodes, cont.Parent);
            if (parentNodes != null)
            {
                TreeNode treeNode = parentNodes.Add( GetContainerName(cont) );
                treeNode.ContextMenuStrip = this.contextMenuStrip;
                treeNode.Tag = cont;
                SetImageIndex(treeNode, cont);

                treeNode.Parent.Expand();
                treeView.SelectedNode = treeNode;
            }
        }

        private static string GetContainerName(Mimoza.ObjectLayer.Container cont)
        {
            string contName = cont.Name;
            if (cont.Parent == null) contName = Transport.LocalInfo.HostName;

            return contName;
        }

        private void UpdateContaner(Mimoza.ObjectLayer.Container cont)
        {
            TreeNode node = FindTreeNodeByContainer(treeView.Nodes, cont);
            if (node != null)
            {
                if (node.Name != cont.Name)
                {
                    node.Text = cont.Name;
                    node.ContextMenuStrip = this.contextMenuStrip;
                    node.Tag = cont;

                   // treeView.SelectedNode = node;
                }
            }
        }
        private void DeleteContaner(Mimoza.ObjectLayer.Container cont)
        {
            TreeNode node = FindTreeNodeByContainer(treeView.Nodes, cont);
            if (node != null)
            {
                TreeNode parent = node.Parent;
                node.Remove();
                parent.Expand();

                treeView.SelectedNode = parent;
            }
        }

        private void ContainerTreeViewModel_OnObjectChanged(object sender, Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs e)
        {
            Mimoza.ObjectLayer.Container container = e.Object as Mimoza.ObjectLayer.Container;
            switch (e.EventType)
            {
                case ObjectLayerEventType.ObjectInserted:
                    Action<Mimoza.ObjectLayer.Container> action = InsertContaner;
                    Invoke(action, container);
                    break;
                case ObjectLayerEventType.ObjectUpdated:
                    Action<Mimoza.ObjectLayer.Container> actionUpdate = UpdateContaner;
                    Invoke(actionUpdate, container);
                    break;
                case ObjectLayerEventType.ObjectDeleted:
                    Action<Mimoza.ObjectLayer.Container> actionDelete = DeleteContaner;
                    Invoke(actionDelete, container);
                    break;
            }
        }
        private void ContainerTreeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Context")
            {
                Mimoza.ObjectLayer.Container cont = m_viewModel.Context as Mimoza.ObjectLayer.Container;
                TreeNode node = FindTreeNodeByContainer(treeView.Nodes, cont);
                if (node != null)
                {
                    treeView.SelectedNode = node;

                    if (cont.Type != ContainerType.Managed)
                    {
                        this.m_viewModel.Application.PolicyViewModel.Visible = false;
                    }
                    else {
                        this.m_viewModel.Application.PolicyViewModel.Visible = true;
                    }
                }
            }
        }
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            m_viewModel.Context = e.Node.Tag;
        }
        private void treeView_Enter(object sender, EventArgs e)
        {
            m_viewModel.InFocus = true;
        }
        private void treeView_Leave(object sender, EventArgs e)
        {
            m_viewModel.InFocus = false;
        }
    }
}
